package com.friend.joey.applicationfriend;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.trace.api.track.HistoryTrackResponse;
import com.friend.joey.applicationfriend.Classes.MyPoint;
import com.friend.joey.applicationfriend.Classes.TimeDeal;
import com.friend.joey.applicationfriend.OldAcitivities.TimePupupWindow;

import java.util.ArrayList;
import java.util.List;

import static com.baidu.location.h.j.t;

/**
 * Created by Joey on 2017/5/18.
 */

public class ShowTrace extends AppCompatActivity{

    private MapView mapView;
    private BaiduMap baiduMap;
    private TraceRecord traceRecord;
    private Polyline mPolyline;
    private TextView starttime;
    private TextView endtime;
    private LinearLayout layout;
    private ImageButton button;
    private TimePupupWindow pupupWindow;
    private TimeDeal timedeal1;
    private TimeDeal timedeal2;
    private long time1;
    private long time2;
    private boolean isFirst;

    @Override
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.c_position );
        mapView = (MapView)findViewById(R.id.bmapView);
        button=(ImageButton)findViewById(R.id.btn_ctrack);
        isFirst=true;
        ////查询历史轨迹
        traceRecord=new TraceRecord("myTrace",getApplicationContext());
        traceRecord.setOnTrackListener(new TraceRecord.onTrackListener() {
            @Override
            public void onNHistoryTrackCallback(HistoryTrackResponse response) {
                if(response.getStatus()==0){
                    Toast.makeText(ShowTrace.this, "查询到轨迹："+Integer.toString(traceRecord.getTotal()), Toast.LENGTH_SHORT).show();
                    if(traceRecord.getTotal()>0){
                        if(isFirst) {
                            initMap(traceRecord.getFirstPoint());
                            DrawLines(traceRecord.getMyPoint());
                            isFirst=false;
                        }else{
                            Toast.makeText(ShowTrace.this, "第二次查询到轨迹："+Integer.toString(traceRecord.getTotal()), Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        initMap(37.5330929990,122.08964860478);
                    }
                }else {
                    Toast.makeText(ShowTrace.this, "查询轨迹失败："+response.getMessage().toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        traceRecord.startCheck();
        ////

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPolyline.remove();
                traceRecord.startCheck(time1,time2);
            }
        });
        timedeal1=new TimeDeal(TimeDeal.INITDATE);
        time1=timedeal1.GetTime();
        timedeal2=new TimeDeal(TimeDeal.INITDATE);
        time2=timedeal2.GetTime();
        starttime=(TextView)findViewById(R.id.txt_ctrackstarttime);
        endtime=(TextView)findViewById(R.id.txt_ctrackendtime);
        starttime.setText(timedeal1.GetAlertTimeString());
        endtime.setText(timedeal2.GetAlertTimeString());
        layout=(LinearLayout)findViewById(R.id.Layout_chooseTime);
        layout.setVisibility(View.VISIBLE);
        starttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pupupWindow=new TimePupupWindow(LayoutInflater.from(ShowTrace.this),getWindow().getDecorView(),timedeal1);
                pupupWindow.setOnPopupWindowListener(new TimePupupWindow.ClickBack() {
                    @Override
                    public void onYesClick(TimeDeal time) {
                        timedeal1=pupupWindow.getTime();
                        String time3=timedeal1.GetAlertTimeString();
                        time1=timedeal1.GetTime();
                        starttime.setText(time3);
                    }
                    @Override
                    public void onNoClick() {

                    }
                });
            }
        });
        endtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pupupWindow=new TimePupupWindow(LayoutInflater.from(ShowTrace.this),getWindow().getDecorView(),timedeal2);
                pupupWindow.setOnPopupWindowListener(new TimePupupWindow.ClickBack() {
                    @Override
                    public void onYesClick(TimeDeal time) {
                        timedeal2=pupupWindow.getTime();
                        String time3=timedeal2.GetAlertTimeString();
                        time2=timedeal2.GetTime();
                        endtime.setText(time3);
                    }
                    @Override
                    public void onNoClick() {

                    }
                });
            }
        });

    }
    private void initMap(MyPoint point){
        //地图相关
        baiduMap = mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        LatLng ll = new LatLng( point.getLatitude(),point.getLongitude());
        MapStatus newMapStatus = new MapStatus.Builder().target(ll).zoom(16).build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(newMapStatus);
        baiduMap.animateMapStatus(mMapStatusUpdate,250);
    }

    private void initMap(double Latitude,double Longitude){
        //地图相关
        baiduMap = mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        LatLng ll = new LatLng( Latitude,Longitude);
        MapStatus newMapStatus = new MapStatus.Builder().target(ll).zoom(16).build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(newMapStatus);
        baiduMap.animateMapStatus(mMapStatusUpdate,250);
    }
    private void DrawLines(List<MyPoint> Points){
        // 构造折线点坐标
        //Toast.makeText(ShowTrace.this, "画线"+Integer.toString(traceRecord.getTotal()), Toast.LENGTH_SHORT).show();
        List<LatLng> points = new ArrayList<LatLng>();
        for(int i=0;i<traceRecord.getTotal();i++)
        {
            points.add(new LatLng(Points.get(i).getLatitude(),Points.get(i).getLongitude()));
        }
//        //构建分段颜色索引数组
//        List<Integer> colors = new ArrayList<>();
//        colors.add(Integer.valueOf(Color.BLUE));
//        colors.add(Integer.valueOf(Color.BLUE));
//        colors.add(Integer.valueOf(Color.BLUE));
//        colors.add(Integer.valueOf(Color.BLUE));
        OverlayOptions ooPolyline = new PolylineOptions().width(7)
                .color(Color.BLUE).points(points);
        //OverlayOptions ooPolyline = new PolylineOptions().width(10).colorsValues(colors).points(points);
        //添加在地图中
        mPolyline = (Polyline) baiduMap.addOverlay(ooPolyline);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
        if(traceRecord.getServeState()) {
            traceRecord.stopTrace();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
        if(traceRecord.getServeState()) {
            traceRecord.stopTrace();
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        if(traceRecord.getServeState()) {
            traceRecord.stopTrace();
        }
    }
}
